<template>
  <div class="powe">
    <!-- 按钮 -->
    <div class="btns">
            <el-button type="primary" size="small" @click="toSave_Up(isShowAll)">保存/更新</el-button>
            <el-button type="primary" size="small" @click="revPower">{{btnStyle}}</el-button>
    </div>
    <!-- /按钮 -->
    <!-- 内容 -->
    <el-table size="small" :data="power">
      <el-table-column label="序号" type="index" :index="1"></el-table-column>
      <el-table-column label="权限名称" prop="name"></el-table-column>
      <el-table-column label="权限编号" prop="id"></el-table-column>
      <el-table-column label="描述" prop="description"></el-table-column>
      <el-table-column label="路由" prop="route" width="120"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template v-slot:default="scope" width="200">
          <el-button type="text" size="mini" @click="deitPower(scope.row)">修改</el-button>
          <!-- <el-button type="text" size="mini" @click="seechildrenPower(scope.row)">查看子权限</el-button> -->
          <el-button type="text" size="mini" @click="delPower(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /内容 -->
    <!-- 模态框 -->
    <el-dialog title="修改权限" :visible.sync="visible" width="60%">
      <el-form label-width="80px" size="small">
        <el-form-item label="权限名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限编号">
          <el-input v-model="form.id"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>

<script>
// 1. 导入请求函数
import {get,post } from '@/utils/request'
export default {
  data(){
    return {
      visible:false,
      param:{
        page:1,
        pageSize:10
      },
      power:[],
      form:{},
      dialogVisible: false,
      isShowAll: false
    }
  },
  // 2.在vue实例创建完毕之后就调用加载用户信息的接口
  created(){
    this.loadPowerData();
  },
  computed: {
      btnStyle () {
          return this.isShowAll ? '显示主要权限' : '显示全部权限'
      }
  },
  methods:{
    deitPower(row){
      // 将要修改的信息赋值给当前表单，进行双向数据绑定，再次基础上完成修改操作
      this.form = row;
      this.visible = true;
    },
    // 删除权限信息
    delPower(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/privilege/deleteById";
        get(url,{id:row.id}).then(response => {
          this.$message({type:"success",message:response.message});
          this.loadPowerData();
        })
      })
    },
    // 提交表单
    submit(){
      // 1. 获取表单数据  this.form
      // 2. 交互
      let url = "/basePower/saveOrUpdate"
      post(url,this.form).then(response => {
        // 3. 提示
        this.$message({type:"success",message:response.message});
        // 4. 关闭模态框
        this.visible = false;
        // 5. 刷新数据
        this.loadPowerData();
      })
    },
    // 加载权限信息
    loadPowerData(){
      let url ="/privilege/findByParentId";
      get(url,this.param).then(response => {
        this.power = response.data;
      })
    },
    //显示全部权限
    revPower(row){
        if (!this.isShowAll) {
            console.log(this.isShowAll)
            this.isShowAll = true
            get("/privilege/findAll").then(response => {
                this.power = response.data;
                // this.loadPowerData();
            })
        } else {
            this.isShowAll = false
            this.loadPowerData()
        }
    },
    //保存更新
    toSave_up(){
        let url = "/privilege/saveOrUpdate";
        post(url,this.form).then(response =>{
            this.$message({type:"success", message:response.message})
            this.power = response.data
            
        })
        this.loadPowerData()
    }
  }
}
</script>
